import { PlayerManager } from "./Biz/PlayerManager";
import { RoomManager } from "./Biz/RoomManager";
import { ApiMsgEnum, IApiGameStartReq, IApiGameStartRes, IApiPlayerJoinReq, IApiPlayerJoinRes, IApiPlayerListReq, IApiPlayerListRes, IApiRoomCreateReq, IApiRoomCreateRes, IApiRoomJoinReq, IApiRoomJoinRes, IApiRoomLeaveReq, IApiRoomLeaveRes, IApiRoomListReq, IApiRoomListRes } from "./Common";
import { Connection } from "./Core/Connections";
import { MyServer } from "./Core/MyServer";
import { symlinkCommon } from "./Utils";
symlinkCommon();

declare module "./Core/Connections"{
    interface Connection{
        playerId:number;
    }
}

const server = new MyServer({
    port:9876
});

server.on("connection",(connection:Connection)=>{
    console.log("加入人数 : "+server.connections.size); 
})

server.on("disconnection",(connection:Connection)=>{
    if(connection.playerId){
        PlayerManager.Instance.removePlayer(connection.playerId); 
    }
    PlayerManager.Instance.syncPlayer();
})
//加入玩家
server.setApi(ApiMsgEnum.ApiPlayerJoin,(connection:Connection,data:IApiPlayerJoinReq):IApiPlayerJoinRes=>{
    const {nickname} = data;
    const player = PlayerManager.Instance.createPlayer({nickname,connection});
    connection.playerId = player.id;

    RoomManager.Instance.syncRooms();
    PlayerManager.Instance.syncPlayer();
    
    return {
        player:PlayerManager.Instance.getPlayerView(player),
    }
})
//创建房间
server.setApi(ApiMsgEnum.ApiRoomCreate,(connection:Connection,data:IApiRoomCreateReq):IApiRoomCreateRes => {
    if(!connection.playerId){
        throw new Error("未登录"); 
    }
    const newRoom = RoomManager.Instance.createRoom();
    const room = RoomManager.Instance.joinRoom(newRoom.id,connection.playerId);
    if(!room){
        throw new Error("房间不存在");
    }
    RoomManager.Instance.syncRooms();
    PlayerManager.Instance.syncPlayer();
    return {
        room:RoomManager.Instance.getRoomView(room)
    }
})
//加入房间
server.setApi(ApiMsgEnum.ApiRoomJoin,(connection:Connection,{rid}:IApiRoomJoinReq):IApiRoomJoinRes => {
    if(!connection.playerId){
        throw new Error("未登录"); 
    }
    const room = RoomManager.Instance.joinRoom(rid,connection.playerId);
    if(!room){
        throw new Error("房间不存在");
    }
    RoomManager.Instance.syncRoom(room.id);
    RoomManager.Instance.syncRooms();
    PlayerManager.Instance.syncPlayer();
    return {
        room:RoomManager.Instance.getRoomView(room)
    }
})
//离开房间
server.setApi(ApiMsgEnum.ApiRoomLeave,(connection:Connection,data:IApiRoomLeaveReq):IApiRoomLeaveRes => {
    if(!connection.playerId){
        throw new Error("未登录"); 
    }
    const player = PlayerManager.Instance.idMapPlayer.get(connection.playerId);
    if(!player){
        throw new Error("玩家不存在");
    }
    if(!player.rid){
        throw new Error("玩家不在房间"); 
    }
    const room = RoomManager.Instance.leaveRoom(player.rid,player.id);
    RoomManager.Instance.syncRoom(player.rid);
    RoomManager.Instance.syncRooms();
    PlayerManager.Instance.syncPlayer();
    return {}
})
//开始游戏
server.setApi(ApiMsgEnum.ApiGameStart,(connection:Connection,data:IApiGameStartReq):IApiGameStartRes => {
    if(!connection.playerId){
        throw new Error("未登录"); 
    }
    const player = PlayerManager.Instance.idMapPlayer.get(connection.playerId);
    if(!player){
        throw new Error("玩家不存在");
    }
    if(!player.rid){
        throw new Error("玩家不在房间"); 
    }
    const room = RoomManager.Instance.startRoom(player.rid);
    RoomManager.Instance.syncRoom(player.rid);
    return {}
})
//玩家列表
server.setApi(ApiMsgEnum.ApiPlayerList,(connection:Connection,data:IApiPlayerListReq):IApiPlayerListRes => {
    return {
        list:PlayerManager.Instance.getPlayersView(),
    }
})
//房间列表
server.setApi(ApiMsgEnum.ApiRoomList,(connection:Connection,data:IApiRoomListReq):IApiRoomListRes => {
    return {
        list:RoomManager.Instance.getRoomsView(),
    }
})

server.start().then(()=>{
    console.log("server start!!");
}).catch((e)=>{
    console.error(e); 
})